use strict;
use utf8;

my %d;
my %words;

binmode(STDIN, ":encoding(utf-8)");
binmode(STDOUT, ":encoding(utf-8)");
binmode(STDERR, ":encoding(utf-8)");

load_dict($ARGV[0]);

while (<STDIN>) {
  chomp $_;
  $_ =~ s/^\s+//g;
  my ($f, $w) = split(/\s+/, $_);
  $w =~ tr/а-яё/А-ЯЁ/;
  $words{$w} += $f;
}

foreach my $w (sort {$words{$b} <=> $words{$a}} keys %words) {
  my $f = $words{$w};
  my @parts = split(/-/, $w);
  
  print "$f\t$w\t";
  if (exists $d{$w}) {
    print join(" / ", @{$d{$w}});
  }
  print "\t";
  foreach my $p (@parts) {
    if (exists $d{$p}) {
      print "$p: " . join(" / ", @{$d{$p}});
    }
    print "\t";
  }
  print "\n";
}

sub load_dict {
  my $fn = shift;

  open(F, "< $fn") || die "can't open $fn";
  binmode(F, ":encoding(utf-8)");
  while (<F>) {
    chomp $_;
    my ($w, $g) = split(/\t/, $_);
    $g =~ s/ /,/g;
    push @{$d{$w}}, $g;
  }
  close(F);
}
